package com.example.home.adapter.provider

import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.provider.BaseItemProvider
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.example.entity.BaseEntity
import com.example.home.R
import com.example.home.adapter.DiscoverItemType
import com.example.home.adapter.SubjectItemAdapter
import com.example.home.databinding.ItemHomeSubjectCardViewBinding
import com.example.home.entity.discover.SubjectCardEntity
import com.example.home.entity.discover.SubjectCardItemData

class SubjectCardProvider : BaseItemProvider<BaseEntity>() {

    private lateinit var subjectItemAdapter : SubjectItemAdapter

    override val itemViewType: Int=DiscoverItemType.SUBJECT_CARD_VIEW
    override val layoutId: Int= R.layout.item_home_subject_card_view

    override fun onViewHolderCreated(viewHolder: BaseViewHolder, viewType: Int) {
      var binding=DataBindingUtil.bind<ItemHomeSubjectCardViewBinding>(viewHolder.itemView)
        //初始化Recview
        binding?.rvCategoryView?.setHasFixedSize(true)
        binding?.rvCategoryView?.layoutManager=GridLayoutManager(viewHolder.itemView.context,2,RecyclerView.HORIZONTAL,false)


    }

    override fun convert(helper: BaseViewHolder, item: BaseEntity) {
        var binding =helper.getBinding<ItemHomeSubjectCardViewBinding>()
        if (item!=null && binding!=null){
            var entity = item as SubjectCardEntity
            binding.tvTitle.text=entity.data.header.title
            binding.tvActionTitle.text=entity.data.header.rightText

            //设置列表数据
            var dataList = mutableListOf<SubjectCardItemData>()
            entity.data.itemList.forEach {
                dataList.add(it.data)
            }

            subjectItemAdapter= SubjectItemAdapter(R.layout.item_home_subject_item_card_view)
            binding.rvCategoryView.adapter=subjectItemAdapter
            subjectItemAdapter.setNewInstance(dataList)
        }
    }


}